import json

from django.db import models
from app01.utils.model import BaseModel
from user.models import User,Comchistory


# Create your models here.
class ComicDetails(models.Model):
    comic_role = models.CharField(max_length=64, verbose_name="主要角色")
    publication_year = models.CharField(max_length=32, verbose_name='出版年份')
    introduction = models.CharField(max_length=32, verbose_name='简介')

    class Meta:
        verbose_name = "动漫详情"
        verbose_name_plural = verbose_name


class ComicCategory(BaseModel):
    name = models.CharField(max_length=64, unique=True, verbose_name="分类名称")

    class Meta:
        db_table = "comic_category"
        verbose_name = "动漫分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.name


class Comic(BaseModel):
    comic_vip = (
        (0, '免费'),
        (1, 'VIP专享'),
    )
    name = models.CharField(max_length=32, verbose_name="动漫名称")
    comic_img = models.ImageField(upload_to="comic_img", max_length=255, verbose_name="封面图片", blank=True, null=True)
    comic_vip = models.SmallIntegerField(choices=comic_vip, default=0, verbose_name="付费类型")
    sections = models.IntegerField(verbose_name="总集数", default=0)
    pub_sections = models.IntegerField(verbose_name="已更新集数", default=0)
    end_if = models.IntegerField(default=0, verbose_name="是否完结", )
    comicdetails = models.OneToOneField(to=ComicDetails, verbose_name='动漫详情', on_delete=models.CASCADE, null=True)
    comic_category = models.ManyToManyField("ComicCategory", db_constraint=False, null=True,
                                            verbose_name="动漫分类")
    comchistorys=models.ManyToManyField(to=Comchistory,db_constraint=False, null=True,
                                            verbose_name="动漫历史",related_name='comchistorys')
    class Meta:
        db_table = "comic"
        verbose_name = "动漫"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.name

    @property
    def comic_detail(self):
        return {"role": self.comicdetails.comic_role, "yser": self.comicdetails.publication_year,
                "introduction": self.comicdetails.introduction}

    def NumofView(self):
        num_list=self.numbeofvisits.all()
        return len(num_list)


    def episodes_list(self):
        comic_chapter_list = self.comicepisodes.all()
        res = []
        for comic_chapter in comic_chapter_list:
            res.append({
                "id": comic_chapter.id,
                "link": comic_chapter.episode_link
            })
        return res




class Comment(models.Model):
    user = models.ForeignKey(to=User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='评论用户')
    comic = models.ForeignKey(to=Comic, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='评论动漫')
    content = models.CharField(verbose_name='评论内容', max_length=255)
    comment_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)
    parent = models.ForeignKey(to='self', null=True, on_delete=models.DO_NOTHING)

    def time(self):
        return self.comment_time.strftime('%Y-%m-%d %H:%M')

    def user_info(self):
        return self.user

    def likes(self):
        res=Likes.objects.filter(commentpk=self.pk).first()
        return res











    ''

    class Meta:
        db_table = "comment"
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s对%s的评论%s" % (self.user, self.comic, self.id)


class ComicEpisode(models.Model):
    comic = models.ForeignKey(to=Comic, related_name='comicepisodes', on_delete=models.DO_NOTHING, db_constraint=False,
                              verbose_name="动漫集")
    orders = models.PositiveSmallIntegerField(verbose_name="集数排序")
    episode_link = models.CharField(max_length=255, null=True, verbose_name="每集链接",
                                    )

    class Meta:
        db_table = "comic_episode"
        verbose_name = "动漫每集"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s-%s" % (self.comic, self.id)

class Likes(models.Model):
    user = models.ManyToManyField(to=User,  db_constraint=False,
                                  verbose_name="点赞用户")
    commentpk=models.OneToOneField(to=Comment,related_name='commentpk',on_delete=models.DO_NOTHING, db_constraint=False,
                              verbose_name="评论",null=True)

    class Meta:
        verbose_name = "点赞"
        verbose_name_plural = verbose_name


class NumbeOfVisits(models.Model):
    comic=models.ForeignKey(to=Comic,related_name='numbeofvisits',on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='动漫id')
    num=models.IntegerField()





